<?php

class DatabaseException extends Exception
{
	protected $sql;
	protected $params;
	
	public function __construct($message, $code = null, $sql = null, $params = null, $backtraceDepth = 0)
	{
		if(strlen($code))
		{
			$message = 'Error ' . $code . ': ' . $message;
		}
		else
		{
			$message = 'Error: ' . $message;
		}
		parent::__construct($message);
		$this->sql = $sql;
		$this->params = $params;
	}
	
	public function getSQL()
	{
		return $this->sql;
	}
	
	public function getParams()
	{
		return $this->params;
	}
	
	public function __toString()
	{
		$message = parent::__toString();
		if($this->sql)
		{
			$message .= "\nSQL Statement:\n\n" . $this->sql . "\n";
			ob_start();
			print_r($this->params);
			$message .= ob_get_clean();
		}
		return $message;
	}
}
